﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.Controller;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Master.Validator;
using RockCompany.Master.View;
using RockCompany.Common.Forms;

namespace RockCompany.Master.View
{
    public partial class SI05A : ParentFrm
    {
        #region Properties
        private Translator trans;
        private SI05AFormModel frmModel;
        private DataGridViewRow selectRow = null;
        private DataTable prodExportDetail;
        private bool isPassed = false;
        private bool isNew = true;
        private SW01B matsWorksManage;
        private string sUserName;
        #endregion

        public SI05A(Translator translator, string sUName, ParentFrm caller)
            : base(sUName, caller)
        {
            InitializeComponent();
            this.trans = translator;
            sUserName = (string.IsNullOrEmpty(sUName) ? "" : sUName);
        }

        #region Event Methods
        private void btClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void button_NewProdExportDetail_Click(object sender, EventArgs e)
        {
            SI04 inventory = new SI04(trans, sUserName, this);
            inventory.callChooser(SI04Const.NO_PROD_TYPE);
            if (inventory.ShowDialog(this) == DialogResult.OK)
            {
                DataTable selectProd = inventory.getSelectedMatsNo();
                inventory.Dispose();
                if (selectProd.Rows.Count > 0 && frmModel.addNewProdExportDetail(sUserName, selectProd))
                    InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
        }

        private void button_DelProdExportDetail_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_DELETE)))
            {
                if (!isNew)
                {
                    if (frmModel.removeProdExportDetail(textBox_ProdExportNo.Text))
                    {
                        InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                    else
                        InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void button_SaveProdExport_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_ADD)))
            {
                if (!string.IsNullOrEmpty(textBox_ProdExportNo.Text))
                {
                    PHIEU_XH_DTO curProdExport = new PHIEU_XH_DTO();
                    curProdExport.MS_PHIEU_XH = textBox_ProdExportNo.Text;
                    curProdExport.KHACH_HANG = tbCustomer.Text;
                    curProdExport.DIA_CHI_KH = tbAddress.Text;
                    curProdExport.KEM_HOA_DON = tbInvoiceNo.Text;
                    curProdExport.PHE_DUYET = isPassed;
                    curProdExport.CREATION_USER = (string.IsNullOrEmpty(sUserName) ? "" : sUserName);
                    curProdExport.LAST_UPDATE_USER = (string.IsNullOrEmpty(sUserName) ? "" : sUserName);

                    bool b1 = false, b2 = false;
                    if (!isNew)
                    {
                        b1 = frmModel.updateProdExport(curProdExport); ;
                        b2 = frmModel.saveData(textBox_ProdExportNo.Text, sUserName);
                        if (b1 && b2)
                        {
                            InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.UpdateComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                        }
                    }
                    else
                    {
                        b1 = (dgvProdductList.Rows.Count > 0 && frmModel.insertNewProdExport(curProdExport));
                        if (b1)
                            b2 = b2 = frmModel.saveData(textBox_ProdExportNo.Text, sUserName);
                        if (b1 && b2)
                        {
                            InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                            isNew = false;
                            textBox_ProdExportNo.Enabled = false;
                        }
                        else
                            InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                    }
                }
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.REQUIRED_FIELD), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
        }

        private void checkBox_Pass_CheckedChanged(object sender, EventArgs e)
        {
            isPassed = checkBox_Pass.Checked;
        }

        private void dgvProdductList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvProdductList.CurrentCell != null && dgvProdductList.CurrentCell.ColumnIndex == 0 && e.RowIndex > -1)
                frmModel.addRemoveCheckedList(0, frmModel.getCheckedRow(e.RowIndex));

        }

        private void dgvProdductList_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int num1_index = dgvProdductList.CurrentRow.Cells[PHIEU_XH_CT_DTO.N_SL_XUAT].ColumnIndex;
            int num2_index = dgvProdductList.CurrentRow.Cells[PHIEU_XH_CT_DTO.N_TRONG_LUONG].ColumnIndex;
            if (e.ColumnIndex == num1_index || e.ColumnIndex == num2_index)
            {
                string s = UserValidator.isValidNumer(e.FormattedValue);
                e.Cancel = !string.IsNullOrEmpty(s);
                if (e.Cancel)
                {
                    InforDialog.showWarningrBox(Owner, trans.getMessageCaption(s), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Utility Methods
        public void initExistData(DataGridViewRow selectRow, DataTable prodExportDetail)
        {
            this.selectRow = selectRow;
            this.prodExportDetail = prodExportDetail;
            isNew = false;
        }

        public void initDefaultData()
        {
            frmModel = new SI05AFormModel();
            trans.controlTranslate(this);
            frmModel.translator = this.trans;

            List<System.Reflection.PropertyInfo> properties = new List<System.Reflection.PropertyInfo>();
            PHIEU_XH_CT_DTO temp1 = new PHIEU_XH_CT_DTO();
            properties.Add(temp1.GetType().GetProperty(PHIEU_XH_CT_DTO.N_CHON));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XH_CT_DTO.N_SO_LO));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XH_CT_DTO.N_MA_MAU));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XH_CT_DTO.N_SL_XUAT));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XH_CT_DTO.N_TRONG_LUONG));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XH_CT_DTO.N_GHI_CHU));

            List<string> readOnlyCols = new List<string>();
            readOnlyCols.Add(PHIEU_XH_CT_DTO.N_SO_LO);
            readOnlyCols.Add(PHIEU_XH_CT_DTO.N_MA_MAU);

            frmModel.buildDataGrid(dgvProdductList, properties, readOnlyCols);
            if (!isNew)
            {
                textBox_ProdExportNo.Text = selectRow.Cells[PHIEU_XH_DTO.N_MS_PHIEU_XH].EditedFormattedValue.ToString();
                textBox_ProdExportNo.Enabled = false;
                tbCustomer.Text = selectRow.Cells[PHIEU_XH_DTO.N_KHACH_HANG].EditedFormattedValue.ToString();
                tbAddress.Text = selectRow.Cells[PHIEU_XH_DTO.N_DIA_CHI_KH].EditedFormattedValue.ToString();
                tbInvoiceNo.Text = selectRow.Cells[PHIEU_XH_DTO.N_KEM_HOA_DON].EditedFormattedValue.ToString();
                isPassed = (bool)selectRow.Cells[PHIEU_XH_DTO.N_PHE_DUYET].EditedFormattedValue;
                checkBox_Pass.Checked = isPassed;

                frmModel.newProdExportDetail = prodExportDetail;
            }
            else
            {
                frmModel.newProdExportDetail = frmModel.buildEmptyDataTable(properties);
            }
            dgvProdductList.DataSource = frmModel.newProdExportDetail;
        }
        #endregion
    }
}
